<?php
require_once ('sn_download_data_functions.php');

/**
 * 
 * Returns the hour of the line
 * @param string $line
 * @return array $values		Writes into the array the hour of the string given
 */
function get_hour($line){
	$token = strtok($line," ");
	$date = $token;
	$time = strtok(" ");
	$token = strtok($time,":");
	$values["hour"] = $token;
	return $values["hour"];
}


/**
 * 
 * Returns the minute of the line
 * @param string $line
 * @return array $values		Writes into the array the minute of the string given
 */
function get_minute($line){
	$token = strtok($line," ");
	$date = $token;
	$time = strtok(" ");
	$token = strtok($time,":");
	$values["hour"] = $token;
	$values["minutes"] = strtok(":");
	return $values["minutes"];
}


/**
 * 
 * Returns the CO2 value of the line
 * @param string $line
 * @return array $values		Writes into the array the CO2 value of the string given
 */
function get_value($line){
	$token = strtok($line," ");
	$date = $token;
	$time = strtok(" ");
	$value = strtok(" ");
	$token = strtok($date,"/");
	$values["day"] = $token;
	$values["month"] = strtok("/");
	$values["year"] = strtok("/");
	$token = strtok($time,":");
	$values["hour"] = $token;
	$values["minutes"] = strtok(":");
	$values["seconds"] = strtok(":");
	$values["value"] = trim($value);
	return $values["value"];
}


/**
 * 
 * Converts the date into the format we need: YYYYMMDD
 * @param string $date
 * @return string $name			
 */
function get_date_format($date){
	$date1 = explode("-", $date);
	$date_year = $date1[0];
	$date_month  = $date1[1];
	$date_day = $date1[2];
	$name = $date_year.$date_month.$date_day;
	return $name;
}


/**
 * 
 * Gets the three params to write the x axis info 
 * @param string $date1
 * @param string $date2
 * @return array $chart_params  (0 -> $start_value, 1 -> $end_value, 2 -> $step)
 */
function get_params($date1, $date2){
	if($date1 == $date2){
		$chart_params[0] = 0;
		$chart_params[1] = 23;
		$chart_params[2] = 2;
	}else{ //convert dates into numbers and compare it to get the difference of the days
		$ts1 = get_timestamp($date1);
		$ts2 = get_timestamp($date2);
		$days = get_days_between($ts1, $ts2);
		$chart_params[0] = 0;
		$chart_params[1] = intval((24 * (intval($days)+1))-1);
		$chart_params[2] = 2 * (intval($days)+1);
	}
	return $chart_params;
}


/**
 * 
 * Gets the timestamp from a date
 * @param string $date
 * @return date $time
 */
function get_timestamp($date){
	$token = strtok($date,"-");
	$year = $token;
	$month = strtok("-");
	$day = strtok("-");
	$time = mktime(0, 0, 0, intval($month), intval($day), intval($year));
	return $time;
}


/**
 * 
 * Gets number of days between two timestamps
 * @param int $ts1
 * @param int $ts2
 * @return int $days
 */
function get_days_between($ts1, $ts2){
	$ts1 = intval($ts1);
	$ts2 = intval($ts2);
	$dif = ($ts2 - $ts1) / 3600;   // 60 x 60 = 3600
	$days = intval($dif/24);
	return $days;
}

/**
 * 
 * Get the co2 points between the days given to draw the chart
 * @param int $days
 * @param string $path
 * @param int $START_DATE
 * @param int $END_DATE
 * @return array $co2_values
 */
function get_points($days, $path, $START_DATE, $END_DATE){
	$pointIndex = 0;
	$mean_value = 0;
	if (is_dir($path)){
		$dir = opendir($path);
		$elements = sort_dir($dir);
		$i=0;
		while ($i < sizeof($elements)){
			$element = $elements[$i];
			$i++;
			$name = get_name($element);
			$extension = get_extension($element);
			if (($START_DATE == $END_DATE) && ($extension == 'txt') && ($name == $START_DATE)){
				$file = fopen("data/$element", "r+");
			}
			else if (($extension == 'txt') && ($name <= $END_DATE) && ($name >= $START_DATE)){
				$file = fopen("data/$element", "r+");
			}
			else continue;
			$k=0;
			while(!feof($file)){
				$line = fgets($file);
				
				//get hour and minute values
				$hour = get_hour($line);
				$minute = get_minute($line);
				
				//if not first and second line continue
				if (($k != 0) && ($k != 1)){
					$min = intval($minute); //0-59
					$value =get_value($line);
					if($value != 0){
						$mean_value = $mean_value + $value/10;
						
						//if ($min % 10 == 0) {
						$var = ($days + 1) * 5;
						if($min % $var == 0){
							$co2_values[$pointIndex] = $value;
							$pointIndex++;
							$mean_value = 0;
						}
					}
				}
				$k++;
			}
			fclose($file);
		}
	}
	closedir($dir);
	return $co2_values;
}

?>